package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.PointsDetailsBo;
import com.rla.domain.vo.PointsDetailsVo;
import com.rla.domain.PointsDetails;
import com.rla.mapper.PointsDetailsMapper;
import com.rla.service.IPointsDetailsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 积分任务明细Service业务层处理
 *
 * @author rla
 * @date 2025-08-30
 */
@RequiredArgsConstructor
@Service
public class PointsDetailsServiceImpl implements IPointsDetailsService {

    private final PointsDetailsMapper baseMapper;

    /**
     * 查询积分任务明细
     */
    @Override
    public PointsDetailsVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询积分任务明细列表
     */
    @Override
    public TableDataInfo<PointsDetailsVo> queryPageList(PointsDetailsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PointsDetails> lqw = buildQueryWrapper(bo);
        Page<PointsDetailsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询积分任务明细列表
     */
    @Override
    public List<PointsDetailsVo> queryList(PointsDetailsBo bo) {
        LambdaQueryWrapper<PointsDetails> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PointsDetails> buildQueryWrapper(PointsDetailsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PointsDetails> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getChangeTime() != null, PointsDetails::getChangeTime, bo.getChangeTime());
        lqw.eq(StringUtils.isNotBlank(bo.getChangeType()), PointsDetails::getChangeType, bo.getChangeType());
        lqw.eq(StringUtils.isNotBlank(bo.getTask()), PointsDetails::getTask, bo.getTask());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), PointsDetails::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getUserId()), PointsDetails::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), PointsDetails::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNumber()), PointsDetails::getOrderNumber, bo.getOrderNumber());
        lqw.eq(bo.getVariablePoints() != null, PointsDetails::getVariablePoints, bo.getVariablePoints());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), PointsDetails::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getNotes()), PointsDetails::getNotes, bo.getNotes());
        lqw.eq(bo.getTaskId() != null, PointsDetails::getTaskId, bo.getTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcCreator()), PointsDetails::getFgcCreator, bo.getFgcCreator());
        lqw.eq(bo.getFgcCreatedate() != null, PointsDetails::getFgcCreatedate, bo.getFgcCreatedate());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcLastmodifier()), PointsDetails::getFgcLastmodifier, bo.getFgcLastmodifier());
        lqw.eq(bo.getFgcLastmodifydate() != null, PointsDetails::getFgcLastmodifydate, bo.getFgcLastmodifydate());
        lqw.eq(bo.getFgcRowversion() != null, PointsDetails::getFgcRowversion, bo.getFgcRowversion());
        lqw.eq(StringUtils.isNotBlank(bo.getFgcUpdatehelp()), PointsDetails::getFgcUpdatehelp, bo.getFgcUpdatehelp());
        return lqw;
    }

    /**
     * 新增积分任务明细
     */
    @Override
    public Boolean insertByBo(PointsDetailsBo bo) {
        PointsDetails add = BeanUtil.toBean(bo, PointsDetails.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改积分任务明细
     */
    @Override
    public Boolean updateByBo(PointsDetailsBo bo) {
        PointsDetails update = BeanUtil.toBean(bo, PointsDetails.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PointsDetails entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除积分任务明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
